package com.netease.nie.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.netease.nie.base.controllers.BaseController;
import com.netease.nie.dto.DrawLotteryActivityDetailDto;
import com.netease.nie.dto.DrawLotteryActivityDto;
import com.netease.nie.dto.DrawLotteryResultDto;
import com.netease.nie.dto.DrawLotterySettingDto;
import com.netease.nie.facade.DrawLotteryFacade;
import com.netease.nie.facade.DrawLotterySettingFacade;

/**
 * 抽奖接口
 * @author gzlibingjie
 * @date 2016-11-22
 */
@RestController
@RequestMapping("/api/draw_lottery_activity")
public class DrawLotteryController extends BaseController {
	
	@Autowired
	private DrawLotteryFacade drawLotteryFacade;
	
	@Autowired
	private DrawLotterySettingFacade drawLotterySettingFacade;
	
	/**
	 * 获取抽奖活动入口配置
	 * @return 抽奖活动入口配置
	 */
	@RequestMapping(value = "/setting", method = RequestMethod.GET, produces = "application/json")
	public String getSetting() {
		DrawLotterySettingDto drawLotterySettingDto = drawLotterySettingFacade.query();
		return jsonSuccess(drawLotterySettingDto);
	}
	
	/**
	 * 获取抽奖活动列表
	 * @return 抽奖活动列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET, produces = "application/json")
	public String listActivity(@RequestParam("page_index") Integer pageIndex) {
		List<DrawLotteryActivityDto> drawLotteryActivityDtoList = drawLotteryFacade.list(pageIndex);
		return jsonSuccess(drawLotteryActivityDtoList);
	}
	
	/**
	 * 获取抽奖活动详情
	 * @param id 抽奖活动ID
	 * @return 活动活动详情
	 */
	@RequestMapping(value = "/detail", method = RequestMethod.GET, produces = "application/json")
	public String getActivity(@RequestParam("id") Integer id) {
		DrawLotteryActivityDetailDto drawLotteryActivityDetailDto = drawLotteryFacade.get(id);
		return jsonSuccess(drawLotteryActivityDetailDto);
	}
	
	/**
	 * 进行抽奖
	 * @param id 抽奖活动ID
	 * @return 抽奖结果
	 */
	@RequestMapping(value = "/draw", method = RequestMethod.POST, produces = "application/json")
	public String draw(HttpServletRequest request, @RequestParam("id") Integer id) {
		Long userId = (Long) request.getAttribute("userId");
		DrawLotteryResultDto drawLotteryResultDto = drawLotteryFacade.draw(id, userId);
		return jsonSuccess(drawLotteryResultDto);
	}
	
}
